/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import Entities.CategoryEntities;
import Entities.CustomConverter;
import Entities.EventEntities;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author vjphl
 */

public class EventDataAccess {
    
    ConnectDatabase objCD = new ConnectDatabase();
    CustomConverter convert = new CustomConverter();
    private List<EventEntities> eventList;
    EventEntities objEvent;
    
    public EventDataAccess(){
        eventList = new ArrayList<EventEntities>();   
    }
    
    public List<EventEntities> getEventAll() throws Exception, ClassNotFoundException{
        eventList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Event_ViewAll()}");
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                objEvent = new EventEntities(objCD.rs.getInt("EventID"), objCD.rs.getString("EventName"), objCD.rs.getDate("StartDate"), objCD.rs.getDate("EndDate"), objCD.rs.getString("Address"), objCD.rs.getString("Images"), objCD.rs.getString("Description"), objCD.rs.getFloat("Price"), objCD.rs.getString("NameWin"), objCD.rs.getInt("CountView"));
                eventList.add(objEvent);
            }
        }
        objCD.close();
        return eventList;
    }
    
    public List<EventEntities> getEventDetail(String EventId) throws Exception, ClassNotFoundException{
        eventList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Event_ViewDetail(?)}");
        objCD.castm.setString(1, EventId);
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                objEvent = new EventEntities(objCD.rs.getInt("EventID"), objCD.rs.getString("EventName"), objCD.rs.getDate("StartDate"), objCD.rs.getDate("EndDate"), objCD.rs.getString("Address"), objCD.rs.getString("Images"), objCD.rs.getString("Description"), objCD.rs.getFloat("Price"), objCD.rs.getString("NameWin"), objCD.rs.getInt("CountView"));
                eventList.add(objEvent);
            }
        }
        objCD.close();
        return eventList;
    }
    
    public List<EventEntities> getEventById(String CatId) throws Exception, ClassNotFoundException{
        eventList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Event_ViewById(?)}");
        objCD.castm.setString(1, CatId);
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                objEvent = new EventEntities(objCD.rs.getInt("EventID"), objCD.rs.getString("EventName"), objCD.rs.getDate("StartDate"), objCD.rs.getDate("EndDate"), objCD.rs.getString("Address"), objCD.rs.getString("Images"), objCD.rs.getString("Description"), objCD.rs.getFloat("Price"), objCD.rs.getString("NameWin"), objCD.rs.getInt("CountView"));
                eventList.add(objEvent);
            }
        }
        objCD.close();
        return eventList;
    }
    
    public boolean Insertevent(EventEntities eventEntities) throws Exception, ClassNotFoundException{
        int check = 0;
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Event_Insert(?,?,?,?,?,?,?,?)}");
        objCD.castm.setInt(1, eventEntities.getCategoryId());
        objCD.castm.setString(2, eventEntities.getEventName());
        objCD.castm.setDate(3, convert.convertUtilDateToSqlDate(eventEntities.getStartDate()));
        objCD.castm.setDate(4, convert.convertUtilDateToSqlDate(eventEntities.getStartDate()));
        objCD.castm.setString(5, eventEntities.getAddress());
        objCD.castm.setString(6, "");
        objCD.castm.setString(7, eventEntities.getDescription());
        objCD.castm.setFloat(8, eventEntities.getPrice());
        check = objCD.doUpdate();
        if (check == 0) {
            return false;
        }
        objCD.close();
        return true;
    }
    
    
    public List<EventEntities> getEventList() {
        return eventList;
    }

    public void setEventList(List<EventEntities> eventList) {
        this.eventList = eventList;
    }
}
